<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
      <!-- <el-row> -->
      <el-form-item label="参数名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入参数名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="北斗定位地址" prop="fourIp">
        <el-input
          v-model="queryParams.fourIp"
          placeholder="请输入北斗定位地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联系统用户" prop="sysUserId">
        <!-- <el-input
          v-model="queryParams.sysUserId"
          placeholder="请输入关联系统用户id"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.sysUserId" clearable placeholder="请选择关联系统用户">
            <el-option
              v-for="item in sysUserIdList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
              <span style="float: left">{{ item.userName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phonenumber }}</span>
            </el-option>
          </el-select>
      </el-form-item>
      <!-- </el-row> -->
      <!-- <el-form-item label="关联北斗定位菜单id" prop="menuId">
        <el-input
          v-model="queryParams.menuId"
          placeholder="请输入关联北斗定位菜单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>  -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:config:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:config:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:config:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:config:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border fit highlight-current-row empty-text="暂无数据" v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" fixed="left" align="center" />
      <!-- <el-table-column label="主键" align="center" fixed="left" prop="id" /> -->
      <el-table-column label="参数名称" align="center" fixed="left" prop="name" />
      <el-table-column label="北斗定位地址" align="center" prop="fourIp" />
      <el-table-column label="北斗用户名" align="center" prop="usernameBD" />
      <el-table-column label="北斗密码" align="center" prop="passwordBD" />
      <el-table-column label="关联系统用户id" align="center" prop="sysUserId" />
      <el-table-column label="系统用户名" align="center" prop="usernameXT" />
      <el-table-column label="配置类型" align="center" prop="type">
        <template slot-scope="scope">
          <span  v-for="(item, i) in typeDataList" :key="i">
            <span v-if="scope.row.type == item.dictValue">{{item.dictLabel}}</span>
          </span>
          
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否关联系统用户 0.否 1.是" align="center" prop="associationStatus" /> -->
      <el-table-column label="是否关联系统用户" align="center" prop="associationStatus">
        <template slot-scope="scope">
          <!-- <span v-if="scope.row.associationStatus == 0">是</span>
          <span v-else-if="scope.row.associationStatus == 1">否</span>
          <span v-else>{{scope.row.associationStatus}}</span> -->
          <span v-if="scope.row.usernameXT">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column label="关联北斗定位菜单id" align="center" prop="menuId" />
      <!-- <el-table-column label="数据删除状态 0.正常 1.停用" align="center" prop="status" /> -->
      <el-table-column label="数据删除状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">正常</span>
          <span v-else-if="scope.row.status == 1">停用</span>
          <span v-else>{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:config:edit']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:config:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改北斗系统参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="fillUpForm" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="参数名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入参数名称" />
            </el-form-item>
            <el-form-item label="北斗定位IP" prop="fourIp">
              <el-input v-model="form.fourIp" placeholder="请输入北斗定位IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="北斗定位接口" prop="fourInterface">
              <el-input v-model="form.fourInterface" placeholder="请输入北斗定位接口" />
            </el-form-item>
            <!-- <el-form-item label="关联系统用户id" prop="sysUserId">
              <el-input v-model="form.sysUserId" placeholder="请输入关联系统用户id" />
            </el-form-item> -->
            <el-form-item label="关联系统用户" prop="sysUserId">
              <el-select v-model="form.sysUserId" placeholder="请选择关联系统用户">
                <el-option
                  v-for="item in sysUserIdList"
                  :key="item.userId"
                  :label="item.userName"
                  :value="item.userId">
                  <span style="float: left">{{ item.userName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phonenumber }}</span>
                </el-option>
              </el-select>
            </el-form-item> 
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联北斗定位菜单id" prop="menuId">
              <el-input v-model="form.menuId" placeholder="请输入关联北斗定位菜单id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="配置类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择配置类型">
                <el-option
                  v-for="item in typeDataList"
                  :key="item.dictCode"
                  :label="item.dictLabel"
                  :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item> 
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/system/fourConfig";
import { listUser } from "@/api/system/user";

import { listData } from "@/api/system/dict/data";

export default {
  name: "Config",
  data() {
    return {
      // 北斗用户名、usernameBD
      // 北斗密码、 passwordBD
      // 系统用户、usernameXT
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 北斗系统参数配置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        fourIp: null,
        fourInterface: null,
        sysUserId: null,
        associationStatus: null,
        menuId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],
        fourIp: [
          { required: true, message: "北斗定位IP不能为空", trigger: "blur" }
        ],
        fourInterface: [
          { required: true, message: "北斗定位接口不能为空", trigger: "blur" }
        ],
        sysUserId: [
          { required: true, message: "关联系统用户不能为空", trigger: "chenge" }
        ],
        // menuId: [
        //   { required: true, message: "关联北斗定位菜单id不能为空", trigger: "blur" }
        // ],
      },
      // 系统用户数据
      sysUserIdList: [],
      typeDataList: [],
    };
  },
  created() {
    this.getList();
    this.getsysUser()

    this.getDictList()
  },
  methods: {
    /** 查询北斗系统参数配置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        fourIp: null,
        fourInterface: null,
        sysUserId: null,
        associationStatus: null,
        menuId: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加北斗系统参数配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.form.sysUserId = Number(this.form.sysUserId)
        this.title = "修改北斗系统参数配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除北斗系统参数配置编号为"' + ids + '"的数据项？').then(function() {
        return delConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    },
    getsysUser(){
      let queryParamsUser = {
        pageNum: 1,
        pageSize: 1000,
      };
      let dateRangeUser = [];
      listUser(this.addDateRange(queryParamsUser, dateRangeUser)).then(response => {
        this.sysUserIdList = response.rows;
      });
    },

    
    /** 查询字典数据列表 */
    getDictList(val) {
      let queryType = {
        pageNum: 1,
        pageSize: 1000,
        dictType: 'beidou_sys_config',
        dictLabel: undefined,
        status: undefined
      }
      listData(queryType).then(response => {
        console.log(response)
        this.typeDataList = response.rows
      })
    },
  }
};
</script>

<style lang="scss" scoped >
  .fillUpForm {
    .el-input{
      width: 100%;
    }
    .el-select{
      width: 100%;
    }
  }
</style>
